jQuery.noConflict();

jQuery(document).ready(function(){	 
    kriesi_tab('#content','.jquery_tab_title','.jquery_tab'); /*remove this if you dont want to have jquery tabs*/
    kriesi_navigation(".nav"); /*remove this if you dont want a jquery sidebar menu*/
    kriesi_closeable_divs(".closeable"); /*remove this if you dont want message box to be closeable*/
    jQuery(".flexy_datepicker, .flexy_datepicker_input").datepicker(); //datepicker input field and box
    //jQuery("#dialog").dialog(); //pop up dialog window on pageopen.
    jQuery('.richtext').wysiwyg(); //rich text editor for textareas
});

function kriesi_closeable_divs(element)
{
    jQuery(element).each(function()							  
    {
        jQuery(this).append('<div class="click_to_close"></div>')
    });	
	
    jQuery(".click_to_close").click(function()
    {
        jQuery(this).parent().slideUp(200);	
    });
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function kriesi_navigation(element)
{
    jQuery(element).each(function()
    {	
        var currentlistitem;
        currentlistitem = jQuery(this).find(">li");
		
        currentlistitem.each(function()
        {	
            if (!jQuery(this).find('ul').hasClass('opened')){
                jQuery(this).find('ul').addClass("closed").css({
                    display:"none"
                });
            }
        });
		
        currentlistitem.find('a:eq(0)').each(function()
        {
            jQuery(this).click(function()
            {	
                if(jQuery(this).next('ul').hasClass('closed')){
                    jQuery(this).next('ul').slideDown(200).removeClass("closed");
                //return false;
                }else{
                    jQuery(this).next('ul').slideUp(200).addClass("closed");	
                //return false;
                }
            });	
        });
    });
}



function kriesi_tab(wrapper, header, content){    
    var title = wrapper + " " + header;
    var container_to_hide = wrapper + " " + content;
    var duration = 200;
	
    if (jQuery.browser.msie){
        duration = 10;
    }
    disable = false;
	

    jQuery(title).each(
        function(i){
            if (i == 0){
                jQuery(wrapper).prepend("<div class='jquery_tab_container'><a href='/' class='heading_tab advanced_link active tab"+(i+1)+"'>"+jQuery(this).html()+"</a></div>");
            }else{
                jQuery(".advanced_link:last").after("<a href='/'class='heading_tab advanced_link tab"+(i+1)+"'>"+jQuery(this).html()+"</a>");
            }
        }
        );
    if (getUrlVars()["tab"]) {
        var tab = getUrlVars()["tab"];
        //alert(tab);
        var cl = ".tab"+(tab);
        jQuery(".advanced_link").removeClass("active");
        jQuery(cl).addClass("active");
    }
    else{
        tab=1;
    }
            
    jQuery(container_to_hide).each(
        function(i){
            jQuery(this).addClass("tablist list_"+i); 
            if(i != (tab-1)){
                jQuery(this).css({
                    display: "none"
                });
            }
        }
        );

    jQuery(".advanced_link").each(
        function(i){
            jQuery( this ).bind ("click",function(){
                if(jQuery(this).hasClass('active')){
                    return false
                }
                if(disable == false){
                    disable = true;
                    jQuery(".advanced_link").removeClass("active");
                    jQuery(this).addClass("active");
														 
                    jQuery(container_to_hide+":visible").fadeOut(duration,function(){
																	
                        jQuery(".list_"+i).fadeIn(duration, function(){
                            disable=false;
                        });
                    });
                }
                return false;

            });
        }
        );
}

function showdetailtrip(abc){
    document.getElementById("basic-modal-content").innerHTML= "<div align='center' style='text-align:center'><img src='images/Blue-011-loading.gif'/></div>";
    var id = abc.id;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("basic-modal-content").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","ajax.php?id="+id,true);
    xmlhttp.send();
    jQuery('#basic-modal-content').modal();
}

function update_trip(abc){
    var id = document.getElementById("id_trip_update").value;
    var status = document.getElementById("selectbox_status_update").value;
    var tripname = document.getElementById("tripname_update_update").value;
    var triptype = document.getElementById("selectbox_trip_type_update").value;
    var placename = document.getElementById("selectbox_place_update").value;
    var des = document.getElementById("textarea_des_update").value;
    var des_file = document.getElementById("textarea_file_update").value;
    var folder_img = document.getElementById("link_image_update").value;
    var numberofpeople = document.getElementById("peoples_update").value;
    
    jQuery.ajax({
        url: "processajax.php",
        type: "POST",
        data: { 
            action: "update",
            id: id, 
            status: status,
            tripname: tripname,
            triptype: triptype,
            placename: placename,
            des: des,
            des_file: des_file,
            folder_img: folder_img,
            numberofpeople: numberofpeople
        },
        success: function(msg){
            if (msg.substr(0, 1)=="1") {
                alert("Cập nhật thành công");
                window.location="index.php";
            }
            else{
                alert("Cập nhật thất bại");
            }
            
        }
    });
    
}

function create_trip(abc){
    var status = document.getElementById("selectbox_status").value;
    var tripname = document.getElementById("tripname_update").value;
    var triptype = document.getElementById("selectbox_trip_type").value;
    var select1 = document.getElementById("selectbox_place1");
    var placename;
    if (select1.style.display=="block" ||select1.style.display=="") {
        placename = document.getElementById("selectbox_place1").value;
    }
    else{
        placename = document.getElementById("selectbox_place2").value;
    }
    var des = document.getElementById("textarea_des").value;
    var des_file = document.getElementById("textarea_file").value;
    var folder_img = document.getElementById("link_image").value;
    var numberofpeople = document.getElementById("peoples").value;
    
    jQuery.ajax({
        url: "processajax.php",
        type: "POST",
        data: { 
            action: "create",
            status: status,
            tripname: tripname,
            triptype: triptype,
            placename: placename,
            des: des,
            des_file: des_file,
            folder_img: folder_img,
            numberofpeople: numberofpeople
        },
        success: function(msg){
            if (msg.substr(0, 1)=="1") {
                alert("Thêm thành công");
                window.location="index.php";
            }
            else{
                alert("Thêm thất bại. Vui lòng xem lại");
            }
            
        }
    });
    
}

function change_placetype(abc){
    var val = abc.value;
    var select1 = document.getElementById("selectbox_place1");
    var select2 = document.getElementById("selectbox_place2");
    if (val=="1") {
        select1.style.display="block";
        select2.style.display="none";
    }
    else{
        select1.style.display="none";
        select2.style.display="block";
    }
}

function load_email(abc){
    document.getElementById("mailbox").innerHTML = "<div align='center' style='text-align:center'><img src='images/Blue-011-loading.gif'/></div>";
    jQuery.ajax({
        url: "processajax.php",
        type: "POST",
        data: {
            action: "load_email"
        },
        success: function(msg){
            document.getElementById("mailbox").innerHTML = msg;
        }
    });
}

function showdetailimage(abc){
    var sym = abc.value;
    if (sym=="-") {
        var rel = confirm("Bạn có muốn đưa hình ảnh ra khỏi gallery không?");
        if (rel) {
            var id = abc.id;
            var status = 0;
            jQuery.ajax({
                url: "processajax.php",
                type: "POST",
                data: { 
                    action: "change_status_img",
                    status: status,
                    id: id
                },
                success: function(msg){
                    if (msg.substr(0, 1)=="1") {
                        alert("Cập nhật thành công");
                        window.location="gallery.php";
                    }
                    else{
                        alert("Cập nhật thất bại. Vui lòng xem lại");
                    }
            
                }
            });
        }
    }
    else{
        var rel = confirm("Bạn có muốn loại bỏ hình ảnh vào gallery không?");
        if (rel) {
            id = abc.id;
            status = 1;
            jQuery.ajax({
                url: "processajax.php",
                type: "POST",
                data: { 
                    action: "change_status_img",
                    status: status,
                    id: id
                },
                success: function(msg){
                    if (msg.substr(0, 1)=="1") {
                        alert("Cập nhật thành công");
                        window.location="gallery.php";
                    }
                    else{
                        alert("Cập nhật thất bại. Vui lòng xem lại");
                    }
            
                }
            });
        }
    }
}

function sendmail(abc){
    var id = abc.id;
    var email_nhan = "email_nhan_"+id;
    var temp1 = document.getElementById(email_nhan).value;
    var content_mail = "content_mail_"+id;
    var temp2 = document.getElementById(content_mail).value;
    var from_name = "from_name_email_"+id;
    var temp3=  document.getElementById(from_name).value;
    document.getElementById("area_mail_send_"+id).innerHTML = "<div align='center' style='text-align:center'><img src='images/Blue-011-loading.gif'/></div>";
    jQuery.ajax({
        url:"processajax.php",
        type: "POST",
        data: {
            action: "send_mail",
            email_nhan: temp1,
            content_mail:temp2,
            from_name: temp3,
            id:id
        },
        success: function(msg){
            if (msg.substr(0, 1)=="1") {
                document.getElementById("area_mail_send_"+id).innerHTML = "Gửi thành công";
                document.getElementById("trangthai_mail_"+id).innerHTML = "Đã trả lời";
                alert("Gửi thành công");
            }
            else{
                document.getElementById("area_mail_send_"+id).innerHTML = "Gửi thất bại";
                alert(msg);
            }
        }
    });
}

function showsendmail(abc){
    var id = abc.id;
    var temp = "tr#area_mail_"+id;
    if (abc.value=="+") {
        jQuery(temp).show();
        abc.value="-";
    }
    else{
        jQuery(temp).hide();
        abc.value="+";
    }
}

function checklogin(abc){
    var username = document.getElementById("name_login").value;
    var password = document.getElementById("password_login").value;    
    jQuery.ajax({
        url: "processajax.php",
        type: "POST",
        data:{
            action: "checklogin",
            username: username,
            password: password
        },
        success: function(msg){
            if (msg.substr(0, 1)=="1") {
                window.location="index.php";
            }
            else{
                jQuery("div#erro_login").fadeIn();
            }
        }
    });
}